//
//   Copyright 2011 Gynvael Coldwind & Mateusz "j00ru" Jurczyk
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
#pragma once

#include <cstdio>
#include <cstdlib>
#include <cstring>
#ifdef __APPLE__
#  include <OpenGL/gl.h>
#  include <OpenGL/glu.h>
#else
#  include <GL/gl.h>
#  include <GL/glu.h>
#endif
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#ifdef _WIN32
 #include <windows.h>
#endif
#include <stdint.h>
#include <math.h>
#include <map>
#include <string>

extern int      screen_width;
extern int      screen_height;
extern bool     keys[SDLK_LAST];
extern uint32_t last_time;
extern float    ratio;

extern int mouse_x, mouse_y;

extern unsigned int textures[100];

unsigned int  make_texture(const char* file_name);
void gynvael_LoadTextures();
void gynvael_DrawMap(int id); // map id in current level
void gynvael_DrawHUD();
void gynvael_MakeAScene();
void gynvael_DrawWall(float dx, float dy, uint32_t surround);
void gynvael_RenderPlayer(uint8_t id);
void gynvael_UpdateObjects();
void gynvael_GetMouse(float *x, float *y);

struct SPRITE_STURCT 
{
  unsigned int *textures;
  int sprite_count;

  float u_max, v_max; // sprites are not resized
};

extern SPRITE_STURCT g_sprites[100];

void make_sprite(int out_sprite_id, const char *file_name, int single_w, int single_h);

#define TEX_BACKGROUND      0
#define TEX_VXLOGO          1
#define TEX_LOSE_SCREEN     2
#define TEX_ABOUT           3
#define TEX_LVL_COMPLETED   4
#define TEX_GAME_COMPLETED  5
#define TEX_BEFORE_LVL      6

#define TEX_TILE_FLOOR 10
#define TEX_TILE_BRIDGE 11

#define TEX_PLAY_BTN   20
#define TEX_ABOUT_BTN  21
#define TEX_EXIT_BTN   22
#define TEX_ARROW      23

#define TEX_PLAYER_TEMP 50
#define TEX_PLAYER_PTR  51
#define TEX_MOUSE_PTR   52
#define TEX_TARGET_PTR  53

#define TEX_BG2         72
#define TEX_BG3         73
#define TEX_BG6         76

#define TEX_TUT_FIRE    80
#define TEX_TUT_TRIGGER    81
#define TEX_TUT_TELE    82
#define TEX_TUT_BRIDGE    83
#define TEX_TUT_STORY    84
#define TEX_TUT_EXIT    85



#define SPRITE_TOGGLE_BUTTON_OFF 0
#define SPRITE_TOGGLE_BUTTON_ON  1
#define SPRITE_END_LEVEL         2
#define SPRITE_EXIT_LEVEL         3

#define SPRITE_PLAYER            10
#define SPRITE_TELEPORT_IN       11
#define SPRITE_TELEPORT_OUT      12


#define SPRITE_SPELL_BULLET        20
#define SPRITE_SPELL_FIREBALL      21

// MIX1: [std button on, std button off, null, null, mouse tile select]
#define SPRITE_MIX1              30

// Vector pointing from active player to mouse.
extern float target_vx, target_vy;

// Index of current floor.
extern int   target_floor_x, target_floor_y;

// Mouse x,y in map (floor) coords.
extern float mmx, mmy;

extern bool lmb, rmb;

void gynvael_GetMouse(float *m_x, float *m_y);
void gynvael_RecalcTarget(float m_x, float m_y);    
void gynvael_GetTeleportDst(int tele_id, int *dx, int *dy, int *dm);

uint32_t ceil_pow2(uint32_t v);

struct FONT_DESC {
  SDL_Surface *font;
  int ch_w, ch_h; // Char width, char height
  int ch_per_line;
  std::map<int,int> ch_to_idx;
  std::map<std::string, unsigned int> text_to_texid;
};

FONT_DESC *fnt_CreateFont(const char *fname, const char *charset,
    int ch_w, int ch_h);

unsigned int fnt_TextToTexture(FONT_DESC *fnt, int w, int h, const char *fmt, ...);

extern FONT_DESC *fonts[10];
extern int g_AboutPage;




